home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 6.4 KB | 215 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWResAcc.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWRESACC_H
- #include "FWResAcc.h"
- #endif
-
- #ifndef FWRESFIL_H
- #include "FWResFil.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #if defined FW_BUILD_MAC && !defined(__RESOURCES__)
- #include <Resources.h>
- #endif
-
- #if defined FW_BUILD_MAC && !defined(__MEMORY__)
- #include <Memory.h>
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FW_ResourceAccess
- #endif
-
- //========================================================================================
- // CLASS FW_CResource
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CResource::FW_CResource
- //----------------------------------------------------------------------------------------
-
- FW_CResource::FW_CResource(const FW_CResourceFile &file,
- FW_ResourceId resourceId,
- FW_ResourceType resourceType) :
- fPrivResourceAccess(new FW_CPrivResourceRep(file, resourceId, resourceType))
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CResource::FW_CResource
- //----------------------------------------------------------------------------------------
-
- FW_CResource::FW_CResource(const FW_CResource& other) :
- fPrivResourceAccess(other.fPrivResourceAccess)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CResource::FW_CResource
- //----------------------------------------------------------------------------------------
-
- FW_CResource::~FW_CResource()
- {
- FW_START_DESTRUCTOR
- }
-
- //========================================================================================
- // CLASS FW_CAcquireResourceData
- //
- // A resource data acquistion helper object. This object can be used in order
- // to obtain access to resource data in an exception safe manner.
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireResourceData::FW_CAcquireResourceData
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireResourceData::FW_CAcquireResourceData(FW_CResource resource) :
- fResource(resource),
- fData(resource.GetData())
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireResourceData::~FW_CAcquireResourceData
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireResourceData::~FW_CAcquireResourceData()
- {
- FW_START_DESTRUCTOR
- fResource.ReleaseData();
- }
-
- //========================================================================================
- // Global Utility Functions
- //========================================================================================
-
- #ifdef FW_BUILD_WIN
- #pragma pack(push,1)
- #endif
-
- struct FW_SMultiStringResEntry
- {
- short fStringId;
- unsigned short fStringOffset;
- };
-
- struct FW_SMultiStringHeader
- {
- unsigned short fStringCount; // number of entrys
- FW_SMultiStringResEntry fEntryArray[1]; // table of entries
- unsigned short GetStringOffset(short theKey);
- };
-
- #ifdef FW_BUILD_WIN
- #pragma pack(pop)
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_SMultiStringHeader::GetStringOffset
- //----------------------------------------------------------------------------------------
-
- static const unsigned short kBadOffset = 0xFFFF;
-
- unsigned short FW_SMultiStringHeader::GetStringOffset(short theKey)
- {
- short wLo = 0,
- wHi = fStringCount - 1;
- short wValue; // trial Value
- // X[wLo..wHi] are ordered, but untested
- do
- {
- short wMid = (wLo + wHi) / 2;
- wValue = fEntryArray[wMid].fStringId; // try middle Value
- if (wValue <= theKey)
- wLo = wMid + 1; // X[0..wLo-1] <= theKey
-
- if (wValue >= theKey)
- wHi = wMid - 1; // X[wHi+1..wMax] >= theKey X[wLo..wHi] untested
- } while (wLo <= wHi); // still at least one untested
-
- if (wLo - wHi == 2)
- return fEntryArray[wHi + 1].fStringOffset;
-
- return kBadOffset;
- }
-
- //========================================================================================
- // Globale Functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByPosition
- //----------------------------------------------------------------------------------------
-
- void FW_FUNC_ATTR FW_LoadStringByPosition(FW_CResourceFile &file,
- FW_ResourceId resourceId,
- FW_ResourceType resourceType,
- unsigned short position,
- FW_CString &string)
- {
- FW_CResource resource(file, resourceId, resourceType);
- void *p = resource.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
- FW_ASSERT(head->fStringCount > 0);
-
- // Compute the Offset into the table, based on the position.
- const unsigned short offset = head->fEntryArray[position].fStringOffset;
-
- string.ReplaceAll(((const FW_Char*)p)+offset);
-
- resource.ReleaseData();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByID
- //----------------------------------------------------------------------------------------
-
- void FW_FUNC_ATTR FW_LoadStringByID(FW_CResourceFile &file,
- FW_ResourceId resourceId,
- FW_ResourceType resourceType,
- unsigned long id,
- FW_CString &string)
- {
- FW_CResource resource(file, resourceId, resourceType);
- void *p = resource.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
- FW_ASSERT(head->fStringCount > 0);
-
- // Compute the Offset into the table, based on the position.
- const unsigned short offset = head->GetStringOffset(id);
- if (offset == kBadOffset)
- FW_ASSERT(FALSE); // throw an exception, I suppose.
-
- string.ReplaceAll(((const FW_Char*)p)+offset);
-
- resource.ReleaseData();
- }
-
-